package com.rafkind.resrev.data;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Reservation implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  
  @ManyToOne
  private User reserver;
  
  @ManyToOne
  private Resource resource;
  
  @ManyToOne
  private User authorizor;
  
  @Column(name="auth_state")
  private Status authorization;
  
  @Temporal(TemporalType.TIMESTAMP)
  private Date creation;
  
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="when_start")
  private Date start;
  
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="when_end")
  private Date end;
  
  
  private Condition beforeCondition;
  private Condition afterCondition;

  public Reservation() {
    authorization = Status.PENDING;
    creation = new Date();
    end = new Date();
    start = new Date();
  } 
  
  public Reservation(User user) {
    reserver = user;
    authorization = Status.PENDING;
    creation = new Date();
    end = new Date();
    start = new Date();
  }
  
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Date getEnd() {
    return end;
  }

  public void setEnd(Date end) {
    this.end = end;
  }

  public Date getStart() {
    return start;
  }

  public void setStart(Date start) {
    this.start = start;
  } 

  public Condition getAfterCondition() {
    return afterCondition;
  }

  public void setAfterCondition(Condition afterCondition) {
    this.afterCondition = afterCondition;
  }

  public Status getAuthorization() {
    return authorization;
  }

  public void setAuthorization(Status authorization) {
    this.authorization = authorization;
  }

  public User getAuthorizor() {
    return authorizor;
  }

  public void setAuthorizor(User authorizor) {
    this.authorizor = authorizor;
  }

  public Condition getBeforeCondition() {
    return beforeCondition;
  }

  public void setBeforeCondition(Condition beforeCondition) {
    this.beforeCondition = beforeCondition;
  }

  public Date getCreation() {
    return creation;
  }

  public void setCreation(Date creation) {
    this.creation = creation;
  }

  public User getReserver() {
    return reserver;
  }

  public void setReserver(User reserver) {
    this.reserver = reserver;
  }

  public Resource getResource() {
    return resource;
  }

  public void setResource(Resource resource) {
    this.resource = resource;
  }
  
  @Override
  public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Reservation)) {
      return false;
    }
    Reservation other = (Reservation) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "com.rafkind.resrev.data.Reservation[id=" + id + "]";
  }
  
  public boolean isNew() {
    return id == null || id == 0;
  }
  
  public boolean overlapsInterval(Date begin, Date finish) {
    return begin.before(end) && finish.after(start);
  }
}